import { request, useUserStore, clearEmpty } from "jmash-core";
import type { AxiosPromise } from "axios";
import type {
  JobTriggerApi,
  JobTriggerReq,
  JobTriggerModel,
  JobTriggerList,
  JobTriggerKey,
  JobTriggerCreateReq,
  JobTriggerUpdateReq,
} from "./types";

class JobTriggerApiImpl implements JobTriggerApi {
  getKey(model: JobTriggerModel): JobTriggerKey {
    return {
      triggerId: model.triggerId,
    } as JobTriggerKey;
  }

  // 查询列表信息任务触发器;
  findList(query?: JobTriggerReq): AxiosPromise<JobTriggerList> {
    const tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/jobs/job_trigger/list/" + tenant,
      method: "get",
      params: query,
    });
  }

  // 查询任务触发器;
  findById(query: JobTriggerKey): AxiosPromise<JobTriggerModel> {
    const tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/jobs/job_trigger/id/" + tenant + "/" + query.triggerId,
      method: "get",
    });
  }

  // 创建实体任务触发器;
  create(data: JobTriggerCreateReq): AxiosPromise<JobTriggerModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/jobs/job_trigger",
      method: "post",
      data: data,
    });
  }

  // 修改实体任务触发器;
  update(data: JobTriggerUpdateReq): AxiosPromise<JobTriggerModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/jobs/job_trigger",
      method: "patch",
      data: data,
    });
  }

  // 删除任务触发器;
  delete(query: JobTriggerKey): AxiosPromise<JobTriggerModel> {
    query.tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/jobs/job_trigger/id",
      method: "delete",
      params: query,
    });
  }
}
const jobTriggerApi = new JobTriggerApiImpl();

export { jobTriggerApi };
